﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Super_Pong
{
    public class Ball : Entity
    {
        Vector2 direction = new Vector2(1, 1);
        
        public Vector2 sortSpeed()
        {
            Random r1 = new Random();
            return new Vector2((float)r1.NextDouble() * 2 + 1, (float)r1.NextDouble() * 2 + 1);
        }

        public Ball(ContentManager content, GameWindow window, String texture_asset_name)
        {

            setTexture(content.Load<Texture2D>(texture_asset_name));
            setPos(new Vector2(window.ClientBounds.Width / 2 - getTexture().Width / 2, window.ClientBounds.Height / 2 - getTexture().Height / 2));
            setSpd(sortSpeed() * direction);
        }

        public void respawn(GameWindow window)
        {
            setPos(new Vector2(window.ClientBounds.Width / 2 - getTexture().Width / 2, window.ClientBounds.Height / 2 - getTexture().Height / 2));
            setSpd(sortSpeed() * direction);
        }

        public BoundingSphere ballCollisionArea()
        {
            return new BoundingSphere(
                new Vector3(
                    getPos().X + getTexture().Width / 2,
                    getPos().Y + getTexture().Height / 2,
                    0),
                    getTexture().Width / 2
                    );
        }

        public void update(GameTime time, GameWindow window, Bar bar1, Bar bar2)
        {
            move(getSpd() * direction);

            //Check collision with the walls
            if (getPos().Y >= window.ClientBounds.Height - getTexture().Height || getPos().Y <= 0)
            {
                direction.Y *= -1;
            }

            //Check collision with the vertical walls (Modify to check collision with bars)


            if (ballCollisionArea().Intersects(bar1.barCollisionArea()) || ballCollisionArea().Intersects(bar2.barCollisionArea()))
            {
                if (ballCollisionArea().Intersects(bar1.barCollisionArea()))
                    setPos(new Vector2(bar1.getPos().X + bar1.getTexture().Width, getPos().Y));
                else
                    setPos(new Vector2(bar2.getPos().X - getTexture().Width, getPos().Y));
                direction.X *= -1;
                accelerate(1.1f);
            }

            if (getPos().X > window.ClientBounds.Width || getPos().X + getTexture().Width < 0)
            {
                if (getPos().X + getTexture().Width < 0)//add point for the player of the left bar 
                {
                    bar2.setPoints(1);
                }
                if (getPos().X > window.ClientBounds.Width)//add point for the player of the right bar
                {
                    bar1.setPoints(1);
                }                
                respawn(window);
            }
        }
    }
}
